 // 顾问电子名片页
import PagePromoterUtil, { PagePromoter } from '../../../data/common/pagePromoter';
import AppSceneUtil from '../../../data/common/appScene';
import SessionUtil, { JxbHttpClient } from '../../../data/session/session';
import SystemUtil from '../../../shell/system/system';
import SellingServerUtil from '../../../data/sellingServer';
import SessionUserUtil from '../../../data/session/sessionUser';
import SessionSellingServerUtil from '../../../data/session/sessionSellingServer';
import InteractionUtil from '../../../shell/ui/interaction';
const Common = require('../../../utils/common.js');
Page({
    data: {
        fromid: -1,
        show: false,
    },
    onLoad: function (options) {
        // 开启转发按钮
        PagePromoterUtil.showShareMenu(true).then()
        InteractionUtil.showLoading()
        SessionUtil.getInstance().then(session=>{
            // 注册当前页面的场景信息
            AppSceneUtil.setPageScene(this)
            let serverId
            if(options.sId || options.salesmanId){
                serverId = options.sId?options.sId:options.salesmanId
            }else if(options.id || options.salesmanVCardId){
                serverId = {salesmanVCardId : options.id ? options.id : options.salesmanVCardId}
            }
            if(serverId){
                return SellingServerUtil.getInstance(serverId)
            }
            return Promise.reject('缺少顾问名片ID')
        }).then(sharerServer => {
            InteractionUtil.hideLoading(1000)
            let sessionUser = SessionUserUtil.getInstanceSync(),
                sessionServer = SessionSellingServerUtil.getInstanceSync()
            if(sessionUser.isSalesman){
                // 顾问角色只找自己买酒
                wx.switchTab({
                    url: '/pages/index/index',
                })
                return
            }
            wx.setNavigationBarTitle({
                title: '顾问信息'
            })
            let sharerVCard = sharerServer.getTheVCardSync(),
                activeVCard = sessionServer.getTheVCardSync()
            this.setData({
                sharerId: sharerServer.salesmanId,
                sharerVCard: {
                    id: sharerVCard.id,
                    name: sharerVCard.name,
                    avatarUri: sharerVCard.avatarUri,
                    contactPhone: sharerVCard.contactPhone,
                    genderId: sharerVCard.genderId,
                    gender: sharerVCard.gender,
                    age: sharerVCard.age,
                    career: sharerVCard.career,
                    jobTitle: sharerVCard.jobTitle,
                    introduction: sharerVCard.introduction
                },
                activeVCard: {
                    id: activeVCard.id,
                    name: activeVCard.name
                },
            })
        }).catch(err=>{
            InteractionUtil.hideLoading()
        })
        
    },
    toIndex: function () {
        wx.switchTab({
            url: '/pages/index/index'
        })
    },
    /**
     * 执行图片的缩放操作
     * @param {*} e 
     */
    scaleImage: function (e) {
        let ratio = e.detail.width / e.detail.height,    //图片的真实宽高比例
            {windowWidth, windowHeight} = SystemUtil.getSystemInfo(false) // 窗口尺寸 
        let viewWidth = windowWidth,           //图片显示宽度
            viewHeight = viewWidth / ratio,    //图片显示高度
            top = (windowHeight - viewHeight) / 2
        this.setData({
            height: viewHeight,
            width: viewWidth,
            top: top
        })
    },
    showHead: function (e) {
        var page = this;
        page.setData({
            show: !page.data.show
        })
    },
    subsection: function (e) {
        // stmp.slice(1, 5))从第2个字符开始，到第5个字符；返回"cinn"
        console.log(e);
        var a = e.slice(0, 3);
        var b = e.slice(3, 7);
        var c = e.slice(7, 11);
        return a + '-' + b + '-' + c + '';
    },
    selectConsultant: function (e) {
        let serverId = e.currentTarget.dataset.salesmanId
        SessionSellingServerUtil.switchSellingServer(serverId).then(message => {
            console.log('切换顾问成功', message)
            wx.showToast({
                title: '顾问切换成功',
                duration: 2000
            })
            Common.when_user_change_clear_the_storage(function () {
                setTimeout(function () {
                    wx.reLaunch({
                        url: '/pages/index/index'
                    })
                }, 1000)
            });
        }).catch(error => {
            console.log(error)
            wx.showToast({
                title: error.message,
                image: '../../images/fail.png',
                duration: 2000
            })
            setTimeout(function () {
                wx.reLaunch({
                    url: '/pages/index/index'
                })
            }, 2000)
        })
    },
    onShareAppMessage: function (res) {
        // 构造并返回自定义数据
        return (new PagePromoter( this.route, { salesmanVCardId : this.data.conid }))
        .setShareTitle('')
        .buildShareInfo()
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    }
});